﻿using SqlSugar;

namespace Repositorys
{
    public class Repository<T> : SimpleClient<T> where T : class, new()
    {
        ////注意：不能写成静态的
        public SqlSugarScope Db;//用来处理事务多表查询和复杂的操作
        public SimpleClient<T> thisDb { get { return new SimpleClient<T>(Db); } }//用来操作当前表的数据
        protected ITenant itenant;//多租户事务



        public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
        {

            if (context == null)
            {
                Db = new SqlSugarScope(new ConnectionConfig()
                {
                    ConnectionString = "server=172.30.199.80;Database=generationcenter;Uid=root;Pwd=Docimax@123",
                    //ConnectionString=ServiceLocatorHelp.IConfiguration.GetSection("mysqlserver").Value,
                    DbType = DbType.MySql,
                    InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
                    IsAutoCloseConnection = true //开启自动释放模式 不设成true要手动close
                }, db => Init(db)) ;
            }
        }
        public void Init(SqlSugarClient db)
        {
            //操作超时
            db.Ado.CommandTimeOut = 30;//单位秒

            db.Aop.DataExecuting = (oldValue, entityInfo) =>
            {
                switch (entityInfo.OperationType)
                {
                    case DataFilterType.UpdateByObject:
                        DefaultEntity.UpdateDefaultEntity(entityInfo);
                        break;
                    case DataFilterType.InsertByObject:
                        DefaultEntity.InsertDefaultEntity(entityInfo);
                        break;
                    default:
                        break;
                }
            };

            LogExecuting.onLogExecuting(db);

            //Filter.onFilter(db);

        }


        /// <summary>
        /// 扩展方法，自带方法不能满足的时候可以添加新方法
        /// </summary>
        /// <returns></returns>
        //public List<T> CommQuery(string json)
        //{
        //    //base.Context.Queryable<T>().ToList();可以拿到SqlSugarClient 做复杂操作
        //    return null;
        //}
    }
}